﻿using PLCS.Shared.Common.Constant;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PLCS.EquipmentService.ParsersHelpers;

public class Utility
{
    public static List<T> FindCommonElements<T>(params List<T>[] lists)
    {
        var res = lists.Where(x => x.Any()).ToArray();
        if (res.Length < 2)
            return null;

        var commonElements = new List<T>(res[0]);

        for (int i = 1; i < res.Length; i++)
        {
            commonElements = commonElements.Intersect(res[i]).ToList();
            if (commonElements.Count == 0)
                return null;
        }

        return commonElements;
    }

    public static List<Station> FindStationsWithCommonTrayTypes(List<string> commonTrayTypes, List<Station> stations)
    {
        var result = new List<Station>();

        foreach (var station in stations)
        {
            var stationTrayTypes = station.MultipleTrayType.Split(ConstantChar.SplitChar).ToList();
            if (commonTrayTypes.All(t => stationTrayTypes.Contains(t)))
            {
                result.Add(station);
            }
        }

        return result;
    }

    /// <summary>
    /// 计算路径中需要走的路程
    /// </summary>
    /// <param name="distances"></param>
    /// <returns></returns>
    public static int CalculatedDistance(List<int> distances)
    {
        int res = 0;
        for (int i = 0; i < distances.Count - 1; i++)
        {
            res += Math.Abs(distances[i] - distances[i + 1]);
        }
        return res;
    }
}

public class MinValueWrapper
{
    public int MinValue { get; set; }
}